/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package helpdesk;

import java.sql.SQLException;
import java.sql.ResultSet;
import java.sql.PreparedStatement;
import java.sql.Connection;

/**
 * Represents a user of the system
 *
 * Only create and update affect the database.
 *
 * @author brian
 */
public class User {
    int id;
    String firstName;
    String lastName;
    String phoneNumber;
    String email;
    String password;
    boolean active;
    boolean support;
    String address;
    String zip;

    public User()
    {
        id = 0;
        firstName = null;
        lastName = null;
        phoneNumber = null;
        email = null;
        password = null;
        active = false;
        support = false;
        address = null;
        zip = null;
    }

    /**
     * Copy u's fields to a new User
     *
     * @param u
     */
    public User(User u)
    {
        id = u.id;
        firstName = u.firstName;
        lastName = u.lastName;
        phoneNumber = u.phoneNumber;
        email = u.email;
        password = u.password;
        active = u.active;
        support = u.support;
        address = u.address;
        zip = u.zip;
    }

    /**
     * Is the user active? Do they have permission to log in?
     * @return
     */
    public boolean getActive() {
        return active;
    }

    /**
     * Activate this user
     * @param active
     */
    public void setActive(boolean active) {
        this.active = active;
    }

    /**
     * Get the user's address.
     * @return
     */
    public String getAddress() {
        return address;
    }

    /**
     * Set the user's address.
     *
     * @param address
     */
    public void setAddress(String address) {
        this.address = address;
    }

    /**
     * Get the user's email.
     * @return
     */
    public String getEmail() {
        return email;
    }

    /**
     * Set the user's email.
     * @param email
     */
    public void setEmail(String email) {
        this.email = email;
    }

    /**
     * Get the user's first name.
     * @return
     */
    public String getFirstName() {
        return firstName;
    }

    /**
     * Set the user's first name.
     * @param firstName
     */
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    /**
     * Get the user's id. This is set by the database on user creation.
     * @return
     */
    public int getId() {
        return id;
    }

    /**
     * Get the user's last name.
     * @return
     */
    public String getLastName() {
        return lastName;
    }

    /**
     * Set the user's last name.
     * @param lastName
     */
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    /**
     * Is this the user's password? We don't want anyone to read the user's
     * password, so we don't have a getter for it.
     * @param pw
     * @return
     */
    public boolean matchPassword(String pw) {
        return password.equals(pw);
    }

    /**
     * Set the user's password.
     * @param password
     */
    public void setPassword(String password) {
        this.password = password;
    }

    /**
     * Get the user's phone number.
     * @return
     */
    public String getPhoneNumber() {
        return phoneNumber;
    }

    /**
     * Set the user's phone number.
     * @param phoneNumber
     */
    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }

    /**
     * Is the user a support user?
     * @return
     */
    public boolean getSupport() {
        return support;
    }

    /**
     * Make the user a support user or regular user.
     *
     * @param support
     */
    public void setSupport(boolean support) {
        this.support = support;
    }

    /**
     * Get the user's zip code.
     * @return
     */
    public String getZip() {
        return zip;
    }

    /**
     * Set the user's zip code
     *
     * @param zip
     */
    public void setZip(String zip) {
        this.zip = zip;
    }

    /**
     * Find user from the database that has the specified email address.
     * @param email
     * @return
     * @throws SQLException
     */
    public static User getUserByEmail(String email) throws SQLException
    {
        Connection          conn;
        PreparedStatement   ps;
        ResultSet           rs;
        User                user=null;

        conn = DBManager.getConnection();
        ps = conn.prepareStatement("SELECT * FROM Users WHERE email=?");
        ps.setString(1, email);
        rs = ps.executeQuery();

        if (rs.next())
        {
            user = new User();
            fill(user, rs);
        }

        return user;
    }

    /**
     * Find user from the database with the specified id.
     * @param id
     * @return
     * @throws SQLException
     */
    public static User getUserById(int id) throws SQLException
    {
        Connection          conn;
        PreparedStatement   ps;
        ResultSet           rs;
        User                user=null;

        conn = DBManager.getConnection();
        ps = conn.prepareStatement("SELECT * FROM Users WHERE userid=?");
        ps.setInt(1, id);
        rs = ps.executeQuery();

        if (rs.next())
        {
            user = new User();
            fill(user, rs);
        }

        conn.close();

        return user;
    }

    /**
     * Used internally for filling in a User object with a database result.
     * @param user
     * @param rs
     * @throws SQLException
     */
    public static void fill(User user, ResultSet rs) throws SQLException
    {
        user.id = rs.getInt("userid");
        user.firstName = rs.getString("firstname");
        user.lastName = rs.getString("lastname");
        user.phoneNumber = rs.getString("phone");
        user.email = rs.getString("email");
        user.password = rs.getString("password");
        user.active = rs.getBoolean("active");
        user.support = rs.getBoolean("support");
        user.address = rs.getString("address");
        user.zip = rs.getString("zip");
    }

    /**
     * Update the user's profile in the database.
     *
     * @throws SQLException
     */
    public void update() throws SQLException
    {
        Connection conn;
        PreparedStatement ps;

        conn = DBManager.getConnection();
        ps = conn.prepareStatement("UPDATE users SET firstname=?, lastname=?, phone=?, email=?, password=?, support=?, address=?, zip=? WHERE userid=?");
        ps.setString(1, firstName);
        ps.setString(2, lastName);
        ps.setString(3, phoneNumber);
        ps.setString(4, email);
        ps.setString(5, password);
        ps.setBoolean(6, support);
        ps.setString(7, address);
        ps.setString(8, zip);
        ps.setInt(9, id);
        ps.executeUpdate();
        conn.commit();
        conn.close();
    }

    /**
     * Create a new user in the database.
     * @throws SQLException
     */
    public void create() throws SQLException
    {
        Connection conn;
        PreparedStatement ps;

        conn = DBManager.getConnection();
        ps = conn.prepareStatement("INSERT INTO users (firstname, lastname, phone, email, password, support, address, zip) VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
        ps.setString(1, firstName);
        ps.setString(2, lastName);
        ps.setString(3, phoneNumber);
        ps.setString(4, email);
        ps.setString(5, password);
        ps.setBoolean(6, support);
        ps.setString(7, address);
        ps.setString(8, zip);
        ps.executeUpdate();
        conn.commit();
        conn.close();
    }
}
